package com.example.sports_meet.volunteer;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

import java.util.Objects;

public class VolunteerSignUp extends AppCompatActivity {

    //内容清空Imagebutton
    private Button aBtnSubmit;
    private EditText etID, etName, etSex, etAge, etEmail;
    //下拉框
    private Spinner spGameType, spGame, spGender;
    String vno, eno, vname, ename;
    MyDbHelper myDbHelper;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volunteer_sign_up);
        Objects.requireNonNull(getSupportActionBar()).hide();
        viewInit();
        myDbHelper = new MyDbHelper(this, "db", null, 2);
        ImageButtonInit();//内容清空Imagebutton
        spinnerinit();//项目选择联动下拉框
        aBtnSubmit.setOnClickListener(new View.OnClickListener() {
            SQLiteDatabase db;
            ContentValues values;

            @Override
            public void onClick(View v) {

                vno = etID.getText().toString();
                vname = etName.getText().toString();
                db = myDbHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("vno", vno);
                values.put("vname", vname);
                values.put("eno", eno);
                values.put("ename", ename);
                db.insert("vapply", null, values);
                Toast.makeText(VolunteerSignUp.this, "报名成功", Toast.LENGTH_SHORT).show();
                db.close();
            }
        });
    }

    //控件初始化
    public void viewInit() {
        aBtnSubmit = findViewById(R.id.btnSubmit);
    }

    //内容清空Imagebutton
    public void ImageButtonInit() {
        etID = findViewById(R.id.etVolunteerID);
        ImageButton deleteOneButton = findViewById(R.id.delete_one);

        deleteOneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                etID.setText("");
            }
        });

        etName = findViewById(R.id.etName);
        ImageButton deleteTwoButton = findViewById(R.id.delete_two);

        deleteTwoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etName.setText("");
            }
        });
        etSex = findViewById(R.id.etGender);
        ImageButton deleteThreeButton = findViewById(R.id.delete_three);
        deleteThreeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etAge.setText("");
            }
        });

        etAge = findViewById(R.id.etAge);
        ImageButton deleteFourButton = findViewById(R.id.delete_four);
        deleteFourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etAge.setText("");
            }
        });

        etEmail = findViewById(R.id.etEmail);
        ImageButton deleteFiveButton = findViewById(R.id.delete_five);
        deleteFiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etEmail.setText("");
            }
        });
    }

    //项目选择下拉框联动
    public void spinnerinit() {
        spGameType = (Spinner) findViewById(R.id.spinnerOne);
        spGame = (Spinner) findViewById(R.id.spinnerTwo);

        adapter = ArrayAdapter.createFromResource(VolunteerSignUp.this, R.array.game_type, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spGameType.setAdapter(adapter);
        spGame.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner) parent;
                String sex = etSex.getText().toString();

                String spGame = (String) spinner.getItemAtPosition(position);
                if (spGame.equals("100米自由泳") && sex.equals("男")) {
                    eno = "202204001";
                    ename = "男子100米自由泳";
                } else if (spGame.equals("100米自由泳") && sex.equals("女")) {
                    eno = "202204002";
                    ename = "女子100米自由泳";
                } else if (spGame.equals("100米仰泳") && sex.equals("男")) {
                    eno = "202204005";
                    ename = "男子100米仰泳";
                } else if (spGame.equals("100米仰泳") && sex.equals("女")) {
                    eno = "202204006";
                    ename = "女子100米仰泳";
                } else if (spGame.equals("水上芭蕾") && sex.equals("女")) {
                    eno = "202204003";
                    ename = "女子水上芭蕾";
                } else if (spGame.equals("水上芭蕾") && sex.equals("男")) {
                    eno = "2022004004";
                    ename = "女子水上芭蕾";
                } else if (spGame.equals("10米气手枪") && sex.equals("男")) {
                    eno = "202204007";
                    ename = "男子10米气手枪";
                } else if (spGame.equals("10米气手枪") && sex.equals("女")) {
                    eno = "202204008";
                    ename = "女子10米气手枪";
                } else if (spGame.equals("飞碟多向") && sex.equals("男")) {
                    eno = "202204010";
                    ename = "男子飞碟多向";
                } else if (spGame.equals("飞碟多向") && sex.equals("女")) {
                    eno = "202204009";
                    ename = "女子飞碟多向";
                } else if (spGame.equals("25米手枪速射") && sex.equals("男")) {
                    eno = "202204011";
                    ename = "男子25米手枪速射";
                } else if (spGame.equals("25米手枪速射") && sex.equals("女")) {
                    eno = "202204012";
                    ename = "女子25米手枪速射";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spGameType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner) parent;
                String gameType = (String) spinner.getItemAtPosition(position);
                ArrayAdapter<CharSequence> gameAdapter = ArrayAdapter.createFromResource(VolunteerSignUp.this, R.array.ssxm, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
                spGame.setAdapter(gameAdapter);
                if (gameType.equals("水上项目")) {
                    gameAdapter = ArrayAdapter.createFromResource(VolunteerSignUp.this, R.array.ssxm, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                } else if (gameType.equals("射击类")) {
                    gameAdapter = ArrayAdapter.createFromResource(VolunteerSignUp.this, R.array.sjl, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                } else if (gameType.equals("田径")) {
                    gameAdapter = ArrayAdapter.createFromResource(VolunteerSignUp.this, R.array.tj, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                } else if (gameType.equals("冰上运动")) {
                    gameAdapter = ArrayAdapter.createFromResource(VolunteerSignUp.this, R.array.bsyd, android.R.layout.simple_spinner_item);
                    spGame.setAdapter(gameAdapter);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
}